﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Tools;
using BLL;
using System.Windows.Forms;
using Model;
using System.IO;
using System.Net;
using System.Text;
using SqlSugar;

namespace DAL
{
    public class ML302DAL
    {
        private UIBLL _UIBLL = new UIBLL();
        private Sql sql = new Sql();
        private Sql sql3 = new Sql("192.168.2.3");
        public string GetWriteCount()
        {
            while (true)
            {
                try
                {
                    int count = sql3.db.Queryable<Sql.t_CMCC_WriteSN>().Where(it => it.idproductionline == "3" && it.prodate > DateTime.Today).Count();
                    int okcount = sql3.db.Queryable<Sql.t_CMCC_WriteSN>().Where(it => it.idproductionline == "3" && it.prodate > DateTime.Today && it.result == "OK").Count();
                    string ztl = "ZTL#(" + count.ToString() + "/" + okcount.ToString() + ") " + Math.Round((float)okcount / count * 100.00, 3) + "%";
                    return ztl;
                }
                catch (Exception)
                {
                    return "err";
                }
            }
        }
        public string GetSysTestCount()
        {
            while (true)
            {
                try
                {
                    int count = sql3.db.Queryable<Sql.t_CMCC_SynTestNew>().Where(it => it.idproductionline == "3" && it.prodate > DateTime.Today).Count();
                    int okcount = sql3.db.Queryable<Sql.t_CMCC_SynTestNew>().Where(it => it.idproductionline == "3" && it.prodate > DateTime.Today && it.result == "OK").Count();
                    string ztl = "ZTL#(" + okcount.ToString() + "/" + count.ToString() + ") " + Math.Round((float)okcount / count * 100.00, 3) + "%";
                    return ztl;
                }
                catch (Exception ee)
                {
                    return "err";
                }
            }
        }
        public string GetParentsn(Label label, string snimei)
        {
            while (true)
            {
                try
                {
                    List<Sql.t_CMCC_ProdSN> prodSNs = sql.db.Queryable<Sql.t_CMCC_ProdSN>().Where(it => it.snimei == snimei).OrderBy(it => it.createtime).ToList();
                    return prodSNs[0].parentsn;
                }
                catch (Exception)
                {
                    AppHelper.Delay(1000);
                    _UIBLL.LabelShowMsg(label, "数据库连接异常，正在重新获取条码");
                }
            }
        }
        public List<Sql.t_CMCC_ProdSN> GetProdList(Label label, string parentsn)
        {
            while (true)
            {
                try
                {
                    List<Sql.t_CMCC_ProdSN> prodSNs = sql.db.Queryable<Sql.t_CMCC_ProdSN>().Where(it => it.parentsn == parentsn && it.issend == 1 && it.status == 1).OrderBy(it => it.createtime).ToList();
                    return prodSNs;
                }
                catch (Exception)
                {
                    AppHelper.Delay(1000);
                    _UIBLL.LabelShowMsg(label, "数据库连接异常，正在重新获取条码");
                }
            }
        }
        string Constr = "server = 192.168.2.3; initial catalog = MESDB; uid=sa;pwd=8ik,9ol.";
        public void WriteSN(Label label, string result, string devcode)//调用写序列号
        {
            while (true)
            {
                try
                {
                    string[] str = result.Split('#');
                    if (str[4].Contains("?"))
                    {
                        return;
                    }
                    string sql = "exec [dbo].[SP_CMCC_WriteSN_Insert] @sn,@productionline,@result,@createuser,@causelog,@devcode";
                    SqlParameter[] pms =
                    {
                    new SqlParameter("@sn",SqlDbType.NVarChar,50),
                    new SqlParameter("@productionline",SqlDbType.Int,50),
                    new SqlParameter("@result",SqlDbType.NVarChar,50),
                    new SqlParameter("@createuser",SqlDbType.NVarChar,50),
                    new SqlParameter("@causelog",SqlDbType.NVarChar,50),
                    new SqlParameter("@devcode",SqlDbType.NVarChar,100),
                    };
                    pms[0].Value = str[4];
                    pms[1].Value = "3";
                    pms[2].Value = str[1];
                    pms[3].Value = "自动线";
                    pms[4].Value = null;
                    pms[5].Value = devcode;
                    DataSet ds = SqlHelper.ExecuteDataset(Constr, CommandType.Text, sql, pms);
                    if (ds.Tables[0].Rows[0][0].ToString()!="1")
                    {
                        AppHelper.Delay(1000);
                        _UIBLL.LabelShowMsg(label, "数据库连接异常，正在重新上传结果");
                        continue;
                    }
                    return;
                }
                catch (Exception ee)
                {
                    AppHelper.Delay(1000);
                    _UIBLL.LabelShowMsg(label, "数据库连接异常，正在重新上传结果");
                }
            }
        }
        public void WriteSynTest(Label label, SysTestModel model)//调用综测
        {
            string sql = "exec [dbo].[SP_CMCC_SynTest_Insert] @sn,@productionline,@result,@createuser,@causelog,@devcodeNew,@param1,";
            SqlParameter[] pms =
            {
            new SqlParameter("@sn",SqlDbType.NVarChar,50),
            new SqlParameter("@productionline",SqlDbType.Int,50),
            new SqlParameter("@result",SqlDbType.NVarChar,50),
            new SqlParameter("@createuser",SqlDbType.NVarChar,50),
            new SqlParameter("@causelog",SqlDbType.NVarChar,50),
            new SqlParameter("@devcodeNew",SqlDbType.NVarChar,50),
            new SqlParameter("@param1",SqlDbType.NVarChar,1000),
            };
            pms[0].Value = model.prodsn;
            pms[1].Value = model.idproductionline;
            pms[2].Value = model.result;
            pms[3].Value = model.createuser;
            pms[4].Value = model.causelog;
            pms[5].Value = model.devcode;
            pms[6].Value = model.param1;
            while (true)
            {
                try
                {
                    DataSet ds = SqlHelper.ExecuteDataset(Constr, CommandType.Text, sql, pms);
                    if (ds.Tables[0].Rows[0][0].ToString() != "1")
                    {
                        AppHelper.Delay(1000);
                        _UIBLL.LabelShowMsg(label, "数据库连接异常，正在重新上传结果1");
                        continue;
                    }
                    return ;
                }
                catch (Exception ee)
                {
                    AppHelper.Delay(1000);
                    _UIBLL.LabelShowMsg(label, "数据库连接异常，正在重新上传结果2");
                    LogHelper.WriteLog("数据库错误： "+ ee.ToString());
                }

            }

        }
        
        
    }
}
